//
//  HMVisitorView.swift
//  WeiBoXR
//
//  Created by acer on 16/9/21.
//  Copyright © 2016年 acer. All rights reserved.
//

import UIKit
protocol HMVisitorViewDelegate : NSObjectProtocol {
    func didClickLoginBtn()
}
class HMVisitorView: UIView {

    var loginClosure: (() -> ())?
    weak var loginDelegate: HMVisitorViewDelegate?
    lazy private var cycleImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy private var maskImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    lazy private var homeImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy private var messageLabel: UILabel = {
       
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.textAlignment = .center
        return label
    }()
    
    lazy private var loginBtn: UIButton = {
        
       let button = UIButton()
        button.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    lazy private var registerBtn: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        return button
    }()

    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        addSubview(cycleImage)
        addSubview(maskImageV)
        addSubview(homeImageV)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        cycleImage.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageV.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImage)
        }
        homeImageV.snp_makeConstraints { (make) in
            make.center.equalTo(cycleImage)
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImage)
            make.top.equalTo(cycleImage.snp_bottom)
            make.width.equalTo(224)
        }
        loginBtn.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
            
        registerBtn.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(loginBtn)
            make.size.equalTo(loginBtn)
        }
    }
    
    func updataVisitorInfo(message: String?, imageName: String?) -> Void {
        
        if let msg = message,let img = imageName {
            
            messageLabel.text = msg
            homeImageV.image = UIImage(named: img)
            cycleImage.isHidden = true
        } else {
            startAnimation()
        }
    }
    
    func startAnimation() -> Void {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.isRemovedOnCompletion = false
        cycleImage.layer.add(animation, forKey: nil)
    }
    
    @objc private func loginBtnClick() {
        print("clickLogin")
        loginClosure?()
        loginDelegate?.didClickLoginBtn()
    }
}
